package gpl.scotlandyard.fugitive.hint.home;

import gpl.scotlandyard.beans.basics.Node;
import gpl.scotlandyard.beans.basics.Player;
import gpl.scotlandyard.fugitive.hint.R;
import gpl.scotlandyard.utils.Scotlandyard;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class PlayerViewFactory {

  public static View create(Context context, int resource, ViewGroup parent, Player player) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View playerView = inflater.inflate(resource, parent, false);

    EditText editName = (EditText) playerView.findViewById(R.id.et_playerName);
    editName.setText(player.getName());
    editName.addTextChangedListener(getNameWatcherOn(player));

    EditText editPosition = (EditText) playerView.findViewById(R.id.et_playerPosition);
    if (player.getNode() == null) {
      editPosition.getText().clear();
    } else {
      editPosition.setText(player.getNode().getNumber() + "");
    }
    if (player.isFugitive()) {
      editPosition.setInputType(InputType.TYPE_CLASS_NUMBER);
      editPosition.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }
    editPosition.setOnFocusChangeListener(getPositionFocusChangeListenerOn(player, editPosition));
    editPosition.addTextChangedListener(getPositionWatcherOn(player, editPosition, context));
    return playerView;
  }

  private static OnFocusChangeListener getPositionFocusChangeListenerOn(final Player player, final EditText editText) {
    return new OnFocusChangeListener() {
      @Override
      public void onFocusChange(View view, boolean focus) {
        if (view == editText && focus) {
          player.setNode(null);
          editText.getText().clear();
        }
      }
    };
  }

  private static TextWatcher getNameWatcherOn(final Player player) {
    return new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        player.setName(s.toString());
      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // do nothing
      }

      @Override
      public void afterTextChanged(Editable s) {
        // do nothing
      }
    };
  }

  private static TextWatcher getPositionWatcherOn(final Player player, final EditText editText, final Context context) {
    return new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().isEmpty()) {
          player.setNode(null);
        } else {
          int node = Integer.valueOf(s.toString());
          if (node >= Scotlandyard.Consts.MIN_NODE && node <= Scotlandyard.Consts.MAX_NODE) {
            player.setNode(new Node(node));
          } else {
            editText.getText().clear();
            player.setNode(null);
            Toast.makeText(context, R.string.node_between_warning, Toast.LENGTH_SHORT).show();
          }
        }
      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // do nothing
      }

      @Override
      public void afterTextChanged(Editable s) {
        // do nothing
      }
    };
  }
}
